package cn.Alibaba.order.service.impl;

import cn.Alibaba.basic.util.LoginContext;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.order.domain.OrderPetAcquisition;
import cn.Alibaba.order.dto.OrderDto;
import cn.Alibaba.order.mapper.OrderPetAcquisitionMapper;
import cn.Alibaba.order.query.OrderPetAcquisitionQuery;
import cn.Alibaba.order.service.IOrderPetAcquisitionService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.domain.Shop;
import cn.Alibaba.org.mapper.EmployeeMapper;
import cn.Alibaba.org.mapper.ShopMapper;
import cn.Alibaba.pet.domain.SearchMasterMsg;
import cn.Alibaba.pet.mapper.PetMapper;
import cn.Alibaba.pet.mapper.SearchMasterMsgMapper;
import cn.Alibaba.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;


    @Override
    public void confirm(OrderDto dto) {


        //
        // 改订单：state = 1
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();

        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);

        orderPetAcquisitionMapper.update(order);

        // 修改寻主为2
        SearchMasterMsg msg = searchMasterMsgMapper.loadOne(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("交易完成 ，成功");
        searchMasterMsgMapper.update(msg);

    }

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {


        Logininfo logininfo = LoginContext.getLoginUser(request);

        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShop_id();
        // 店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.loadOne(shopId);

            // 否则就是店员
            if (shop.getAdmin_id()!=employee.getId()){
                query.setEmpId(employee.getId());
            }

            query.setShopId(shopId);

        }
        return super.queryPage(query);
    }

    @Override
    public void del(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(id);
        //将寻主消息中的shop_id置空-  寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //删除宠物 - 没有成功收购
        petMapper.delete(order.getPetId());
        //删除订单
        //super.del(id);
    }
}
